﻿#include "lvgl_hint.h"
#include "lvgl_Project.h"
#include "lvgl_hint_anim.h"

#if 0
#define lvgl_hint_debug(format, ...) lvgl_project_debug("[hint]- ",format,##__VA_ARGS__);
#else
#define lvgl_hint_debug(format, ...) ;
#endif

_lvgl_hint_data lvgl_hint_data;

lv_style_t lvgl_hint_style;

void lvgl_hint_TaskCb(lv_task_t* t)
{
    if (lvgl_hint_data.delete_flag == false)
    {
        if (--lvgl_hint_data.shijian <= 0)
        {
            lvgl_hint_data.delete_flag = true;
            lvgl_hint_anim_out_start(0, 0);
        }
    }
}


void lvgl_hint_event_cb(struct _lv_obj_t* obj, lv_event_t event)
{

    if (obj == lvgl_hint_data.lvgl_btn_exit)
    {
        switch (event)
        {
        case LV_EVENT_DRAG_END://拖动结束后

            lvgl_hint_close();
            break;

        }
    }
    else
        if (obj == lvgl_hint_data.cont_main)
        {
            //lvgl_hint_debug("cb:%d\r\n", event);
            switch (event)
            {

            case LV_EVENT_DELETE:

                lvgl_hint_data.cont_main = NULL;
                lvgl_hint_debug("删除窗口\r\n");
                break;

            case LV_EVENT_PRESSING://对象被持续按下


                break;
            case LV_EVENT_PRESSED://对象已被按下


                //lvgl_hint_anim_chu();


                break;
            case LV_EVENT_DRAG_END://拖动结束后

                lvgl_hint_close();
                break;

            case LV_EVENT_RELEASED://按钮释放

                //lvgl_hint_anim_chu();

                break;

            case LV_EVENT_LONG_PRESSED://按钮长按

                break;

            default:
                break;

            }
        }
}


void lvgl_hint_create(lv_obj_t* Fu, char* text, uint8_t touming, uint8_t shijian)
{

    lvgl_hint_data.delete_flag = false;
    if (lvgl_hint_data.cont_main == NULL)
    {


        lv_style_init(&lvgl_hint_style);
        lv_style_copy(&lvgl_hint_style, &lvgl_style.cont_transparent);

        lv_style_set_radius(&lvgl_hint_style, LV_STATE_DEFAULT, 5);//圆角
        /*背景色*/
        lv_style_set_bg_opa(&lvgl_hint_style, LV_STATE_DEFAULT, touming);//背景透明度
        lv_style_set_bg_color(&lvgl_hint_style, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x00, 0x00, 0x00));//背景上面颜色
        lv_style_set_bg_grad_color(&lvgl_hint_style, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x00, 0x00, 0x00));//背景上面颜色
        lv_style_set_bg_grad_dir(&lvgl_hint_style, LV_STATE_DEFAULT, LV_GRAD_DIR_VER);//渐变方向

        //----创建容器----//
        lvgl_hint_data.cont_main = lv_cont_create(Fu, NULL);
        lv_obj_set_size(lvgl_hint_data.cont_main, 80, 24);
        lv_obj_set_pos(lvgl_hint_data.cont_main, (lv_obj_get_width(lv_scr_act()) - lv_obj_get_width(lvgl_hint_data.cont_main)) / 2, lv_obj_get_height(lv_scr_act()));


        lv_obj_set_click(lvgl_hint_data.cont_main, true); //启用 / 禁用可点击
        lv_obj_set_drag(lvgl_hint_data.cont_main, true);//启用/禁用对象可拖动
        lv_obj_set_drag_dir(lvgl_hint_data.cont_main, LV_DRAG_DIR_BOTH);//设置拖动方向
        lv_obj_set_drag_throw(lvgl_hint_data.cont_main, true);//启用/禁用拖动后是否有惯性移动

        lv_obj_set_drag_parent(lvgl_hint_data.cont_main, false); //启用 / 禁用父对象可拖动
        lv_obj_add_style(lvgl_hint_data.cont_main, LV_OBJ_PART_MAIN, &lvgl_hint_style);//设置样式
        lv_obj_set_event_cb(lvgl_hint_data.cont_main, lvgl_hint_event_cb);//设置回调函数


        lvgl_hint_data.lvgl_Label_text = lv_label_create(lvgl_hint_data.cont_main, NULL);
        lv_label_set_long_mode(lvgl_hint_data.lvgl_Label_text, LV_LABEL_LONG_SROLL_CIRC);//循环滚动
        lv_obj_set_pos(lvgl_hint_data.lvgl_Label_text, 0, 0);
        lv_obj_set_width(lvgl_hint_data.lvgl_Label_text, lv_obj_get_width(lvgl_hint_data.cont_main));
        lv_obj_set_height(lvgl_hint_data.lvgl_Label_text, lv_obj_get_height(lvgl_hint_data.cont_main));
        lv_label_set_align(lvgl_hint_data.lvgl_Label_text, LV_LABEL_ALIGN_CENTER);
        lv_label_set_recolor(lvgl_hint_data.lvgl_Label_text, true);

        lv_obj_add_style(lvgl_hint_data.lvgl_Label_text, LV_OBJ_PART_MAIN, &lvgl_style.font_changyong_16_white_bg);

        lv_label_set_anim_speed(lvgl_hint_data.lvgl_Label_text, 20);

        lv_obj_align(lvgl_hint_data.lvgl_Label_text,NULL, LV_ALIGN_CENTER, 0, 0);


        lvgl_hint_data.lvgl_btn_exit = lv_btn_create(lvgl_hint_data.cont_main, NULL);
        lv_obj_set_pos(lvgl_hint_data.lvgl_btn_exit, 0, 0);
        lv_obj_set_size(lvgl_hint_data.lvgl_btn_exit, lv_obj_get_width(lvgl_hint_data.cont_main), lv_obj_get_height(lvgl_hint_data.cont_main));

        lv_obj_set_click(lvgl_hint_data.lvgl_btn_exit, true); //启用 / 禁用可点击

        lv_obj_add_style(lvgl_hint_data.lvgl_btn_exit, LV_OBJ_PART_MAIN, &lvgl_style.cont_transparent);
        lv_obj_set_drag_parent(lvgl_hint_data.lvgl_btn_exit, true); //启用 / 禁用父对象可拖动

        lv_obj_set_event_cb(lvgl_hint_data.lvgl_btn_exit, lvgl_hint_event_cb);//设置回调函数


        lvgl_hint_debug("create win\r\n");
    }
    else
    {
        lvgl_hint_debug("show win\r\n");
    }

    lv_label_set_text(lvgl_hint_data.lvgl_Label_text, text);

    lv_obj_set_pos(
        lvgl_hint_data.cont_main,
        (lv_obj_get_width(lv_scr_act()) - lv_obj_get_width(lvgl_hint_data.cont_main)) / 2,
        lv_obj_get_height(lv_scr_act())
    );

    lvgl_hint_data.shijian = shijian;
    lvgl_set_obj_show(&lvgl_hint_data.cont_main);
    lv_obj_move_foreground(lvgl_hint_data.cont_main);

    lvgl_task_create(&lvgl_hint_data.lvgl_task, lvgl_hint_TaskCb, 1000, LV_TASK_PRIO_HIGH, NULL);
    lvgl_hint_anim_in_start(0, 0);
}

void lvgl_hint_close(void)
{
    lvgl_hint_debug("关闭窗口\r\n");
    lvgl_task_delete(&lvgl_hint_data.lvgl_task);
    //lvgl_obj_clean(&lvgl_hint_data.cont_main);
    //lvgl_obj_del(&lvgl_hint_data.cont_main);

    lvgl_set_obj_hide(&lvgl_hint_data.cont_main);

}


void lvgl_hint_set_top(void)
{
	if(lvgl_hint_data.cont_main == NULL)
	{
		return;
	}
	lv_obj_move_foreground(lvgl_hint_data.cont_main);
}
